import type { SysAttachment } from "@/pages/system/attachment/entity";
import type { Result } from "#/api";
import apiClient from "../../apiClient";

export interface PageListReq {
	fileName?: string;
	fileType?: string;
	page: number;
	pageSize: number;
}

export interface PageListRes {
	total: number;
	records: SysAttachment[];
}

const pageList = (data: PageListReq) => apiClient.post<PageListRes>({ url: "/system/attachment/pageList", data });
const getInfo = (id: React.Key) => apiClient.get<SysAttachment>({ url: `/system/attachment/${id}` });
const add = (data: SysAttachment) => apiClient.post<SysAttachment>({ url: "/system/attachment", data });
const update = (data: SysAttachment) => apiClient.post<SysAttachment>({ url: "/system/attachment", data });
const remove = (ids: React.Key[]) => apiClient.delete({ url: "/system/attachment", params: { ids: ids.join(",") } });

//上传文件
const uploadFile = (file: File) => {
	const formData = new FormData();
	formData.append("file", file);

	return apiClient.post<Result<any>>({
		url: "/system/attachment/upload",
		data: formData,
		headers: {
			// 不要手动设置Content-Type，让浏览器自动处理
			// 浏览器会自动设置正确的Content-Type和boundary
		},
	});
};

//下载文件
const downloadFile = (attachmentId: React.Key) => {
	apiClient.get<any>({ url: `/system/attachment/download/${attachmentId}` });
};

export default {
	pageList,
	getInfo,
	add,
	update,
	remove,
	uploadFile,
	downloadFile,
};
